﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace University.Workshop.DataAccess
{
    public class DataAccessOtbData
    {
        public static void RegisterOtbData(Model.OtbData otbData)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.OtbData.Add(otbData);
            WFPEntities.Instance.SaveChanges();
        }

        public static void ModifyOtb(Model.OtbData otbData)
        {
            WFPEntities.Instance.DiscardChanges();
            var otb = (from o in WFPEntities.Instance.OtbData
                       where o.Id == otbData.Id
                       select o).First();
            otb.Managment = otbData.Managment;
            otb.Name = otbData.Name;
            otb.Background = otbData.Background;
            otb.ExpirationDay = otbData.ExpirationDay;
            otb.PresidentId = otbData.PresidentId;

            WFPEntities.Instance.SaveChanges();
        }

        public static ICollection< Model.OtbData> GetCurrentOtbData()
        {
            return (from o in WFPEntities.Instance.OtbData
                     orderby o.Id descending
                     select o).ToList();

        }

        public static ICollection<Model.OtbData> SearchOtbDataByParameter(Model.OtbData otb)
        {
            return (from o in WFPEntities.Instance.OtbData
                    where (o.Managment.Contains(otb.Managment) &&
                            o.Name.StartsWith(otb.Name) &&
                            o.Person.Names.StartsWith(otb.Person.Names) &&
                            o.Person.LastNames.StartsWith(otb.Person.LastNames))
                    select o).ToList();
        }
    }
}
